Skip to main content

Simple Telegram Bot for Exchange using Flashift API

Introduction

This guide explains how to create a Telegram bot using Python that allows users to exchange cryptocurrencies using the Flashift API. The bot will:

  • Retrieve supported exchange providers
  • Get estimated exchange amounts
  • Initiate transactions
  • Provide transaction status updates

Prerequisites

  • Python 3.x installed
  • A Telegram bot token (generated via BotFather)
  • Flashift API Key
  • Required Python libraries:
    pip install python-telegram-bot requests

Step 1: Create a Telegram Bot

  1. Open Telegram and search for BotFather.
  2. Type /newbot and follow the instructions.
  3. Copy the bot token provided.
  4. Save it as an environment variable or in your script.

Step 2: Setting Up Flashift API

Obtain API Key

  • Sign up on Flashift and get an API Key.
  • Store the API Key securely.

Step 3: Writing the Telegram Bot Code

1. Initialize the Bot

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import requests
import os

# Load API keys
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
FLASHIFT_API_KEY = "YOUR_FLASHIFT_API_KEY"

updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher

2. Get Supported Providers

def get_providers():
url = "https://interfacev2.flashift.app/api/dev/v2/getProviders"
headers = {"Authorization": FLASHIFT_API_KEY}
response = requests.get(url, headers=headers)
return response.json() if response.status_code == 200 else {}

3. Get Estimated Exchange Amount

def get_estimated_amount(symbol_from, network_from, symbol_to, network_to, amount):
url = "https://interfacev2.flashift.app/api/dev/v2/getEstimatedAmount"
params = {"symbol_from": symbol_from, "network_from": network_from, "symbol_to": symbol_to, "network_to": network_to, "amount": amount}
headers = {"Authorization": FLASHIFT_API_KEY}
response = requests.get(url, headers=headers, params=params)
return response.json() if response.status_code == 200 else {}

4. Create an Exchange Transaction

def create_transaction(provider, symbol_from, network_from, symbol_to, network_to, to_address, amount):
url = "https://interfacev2.flashift.app/api/dev/v2/createTransaction"
headers = {"Authorization": FLASHIFT_API_KEY, "Content-Type": "application/json"}
data = {
"provider_name": provider,
"symbol_from": symbol_from,
"network_from": network_from,
"symbol_to": symbol_to,
"network_to": network_to,
"to_address": to_address,
"amount": str(amount),
"fixed": False
}
response = requests.post(url, headers=headers, json=data)
return response.json() if response.status_code == 200 else {}

5. Handle Commands in Telegram Bot

/start Command

def start(update: Update, context: CallbackContext):
update.message.reply_text("Welcome to Crypto Exchange Bot! Use /exchange to swap crypto.")

dispatcher.add_handler(CommandHandler("start", start))

/providers Command

def providers(update: Update, context: CallbackContext):
data = get_providers()
message = "Available Providers:\n" + "\n".join([p['name'] for p in data])
update.message.reply_text(message)

dispatcher.add_handler(CommandHandler("providers", providers))

/estimate Command

def estimate(update: Update, context: CallbackContext):
if len(context.args) < 5:
update.message.reply_text("Usage: /estimate BTC mainnet ETH mainnet 0.1")
return

symbol_from, network_from, symbol_to, network_to, amount = context.args
data = get_estimated_amount(symbol_from, network_from, symbol_to, network_to, amount)
message = f"Estimated {amount} {symbol_from} ({network_from}) -> {data.get('best_amount', 'N/A')} {symbol_to} ({network_to})"
update.message.reply_text(message)

dispatcher.add_handler(CommandHandler("estimate", estimate))

/exchange Command

def exchange(update: Update, context: CallbackContext):
if len(context.args) < 6:
update.message.reply_text("Usage: /exchange provider BTC mainnet ETH mainnet wallet_address 0.1")
return

provider, symbol_from, network_from, symbol_to, network_to, to_address, amount = context.args
data = create_transaction(provider, symbol_from, network_from, symbol_to, network_to, to_address, amount)
message = f"Transaction Created! ID: {data.get('exchange_id', 'N/A')}"
update.message.reply_text(message)

dispatcher.add_handler(CommandHandler("exchange", exchange))

6. Start the Bot

updater.start_polling()
updater.idle()

Conclusion

You now have a Telegram bot that can retrieve providers, estimate exchange rates, and execute cryptocurrency swaps using the Flashift API. You can extend this bot by adding:

  • Error Handling for better user experience
  • Transaction Tracking using /getTransactionInfo
  • Webhook Support for real-time updates